package com.example.tbag.healthcare.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.example.tbag.healthcare.Adapter.MedecineListAdapter;
import com.example.tbag.healthcare.Model.Cache;
import com.example.tbag.healthcare.R;
import com.example.tbag.healthcare.Util.CallPhoneUtil;
import com.example.tbag.healthcare.Util.DatabaseHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;



public class SearchDragActivity extends AppCompatActivity {
    private DatabaseHelper helper = new DatabaseHelper(this);
    private List<Cache> caches = new ArrayList<>();
    @BindView(R.id.list)
    ListView listView;
    @BindView(R.id.text)
    EditText editText;
    
    
    private MedecineListAdapter adapter;

    @OnClick(R.id.btn_back)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.left_btn1)
    public void search(){
        caches.clear();
        getPersons(editText.getText().toString());
        adapter.notifyDataSetChanged();
    }


    public void getPersons(String  text) {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("med",null,"name like ?",new String[]{"%"+text+"%"},null,null,null);
        if (cursor != null && cursor.moveToFirst()) {
            caches.add(getHumanInfo(cursor));
            while (cursor.moveToNext()) {
                caches.add(getHumanInfo(cursor));
            }
            cursor.close();
            db.close();
        }
    }

    public Cache getHumanInfo(Cursor cursor) {
        String date = cursor.getString(cursor.getColumnIndex("name"));
        String time = cursor.getString(cursor.getColumnIndex("money"));
        int id = cursor.getInt(cursor.getColumnIndex("_id"));
        Cache humanInfo = new Cache(date, time,id);
        return humanInfo;
    }


    @OnClick(R.id.right_btn)
    public void callForHelp() {
        CallPhoneUtil.call(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initListView();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initListView() {
        adapter = new MedecineListAdapter(caches, this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SearchDragActivity.this,AddMedecineActivity.class);
                intent.putExtra("id",caches.get(position).id);
                intent.putExtra("type",0);
                startActivity(intent);
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                        //定义AlertDialog.Builder对象，当长按列表项的时候弹出确认删除对话框  
                        AlertDialog.Builder builder = new AlertDialog.Builder(SearchDragActivity.this);
                        builder.setMessage("确认删除该体征么");
                        builder.setTitle("提示");
                        //添加AlertDialog.Builder对象的setPositiveButton()方法  
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                delete(caches.get(position));
                                caches.remove(position);
                                adapter.notifyDataSetChanged();
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        builder.create().show();
                        return false;
                    }
                });

                return false;
            }
        });
    }

    private void delete(Cache person) {
        helper.getWritableDatabase().delete("med", "_id=?", new String[]{person.id+""});
    }

    
}
